<?php

namespace app\model;

use app\api\service\WechatSubscribeService;
use think\facade\Queue;
use think\Model;
use think\model\relation\BelongsTo;

class VideoDetails extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'video_details';
    // 设置当前模型主键
    protected $pk = 'id';

    protected $json = ['platform_config'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    /**
     * 根据条件获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param string fields
     * @param array where
     * @param string order
     * @return array
     */
    public function getInfoByWhere($where = [], string $fields = 'id', string $order = 'sort asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 根据videoId获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param int videoId
     * @param string fields
     * @param string order
     * @return array
     */
    public function getAllDataByVideoId($videoId, string $fields = 'id', string $order = 'sort asc', $limit = 0)
    {
        $model = $this::where(['video_id' => $videoId, 'is_delete' => 0])->field($fields)->order($order);
        if ($limit) {
            $model->limit($limit);
        }
        $data = $model->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 根据Id获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param int id
     * @param string fields
     * @return array
     */
    public function getInfoById($id, $fields = 'id')
    {
        $data = $this::where(['id' => $id])->field($fields)->find();
        return $data ? $data->toArray() : [];
    }

    public function getCacheInfoById($id, $fields = 'id')
    {
        $data = $this::where(['id' => $id])->field($fields)->cache(true,60)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 根据Ids获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param array ids
     * @param string fields
     * @return array
     */
    public function getInfoByIds(array $ids, string $fields = 'id', string $order = 'id desc')
    {
        $data = $this::where(['id' => $ids])->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 根据videoIds获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param array videoIds
     * @param string fields
     * @return array
     */
    public function getInfoByVideoIds(array $videoIds, string $fields = 'id', string $order = 'id desc', string $group = '')
    {
        $model = $this::where(['video_id' => $videoIds])->field($fields);
        if ($group) {
            $model->group($group);
        }
        $data = $model->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    // 根据条件获取分页数据
    public function getPagingDataByWhere($where = [], $page = 1, $limit = 10, $fields = 'id, name', $order = 'sort asc')
    {
        $total = $this::where($where)->count();

        if ($total == 0) {
            return ['total' => 0, 'total_page' => 0, 'data' => []];
        }

        $offset = ($page - 1) * $limit;
        $totalPage = ceil($total / $limit);

        $data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();

        return ['total' => $total, 'total_page' => $totalPage, 'data' => $data];
    }

    /**
     * 添加/编辑
     * 
     * @dateTime 2022/09/19
     * @author zhixiang0215@126.com
     * @param array $params
     * @return int
     */
    public function edit(array $params)
    {
        if (empty($params['id'])) {
            $data = $this::create($params);
            return $data->id;
        }
        $this::update($params);
        return $params['id'];
    }

    /**
     * 根据条件获取数据
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param string fields
     * @param array where
     * @param string order
     * @return array
     */
    public function getDataByWhere($where = [], string $fields = 'id', $order = 'sort asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    /**
     *
     * @return BelongsTo
     */
    public function video(): BelongsTo
    {
        return $this->belongsTo('app\model\Videos', 'video_id', 'id');
    }

    /**
     * 新增后操作
     * @param VideoDetails $model
     * @return void
     */
    public static function onAfterInsert(VideoDetails $model) {
        try {
            //上新后订阅消息推送
            $data = [
                'type'  => WechatSubscribeService::TYPE_VIDEO_UPDATE,//类型
                'thing1' => $model->video->name ?? '',//剧集标题
                'thing2' => $model->toArray()['name'] ?? '',//更新集数
                'time3' => date('Y-m-d H:i:s',$model->created_at),//更新时间
                'thing4' => $model->video->synopsis ?? '',//温馨提醒
                'page' => "/pages/index/index?video_id={$model->video->id}&video_detail_id={$model->id}&source=link",//跳转地址
            ];
            Queue::push('app\job\WechatSubscribeSendJob', $data, 'wechat-subscribe-send');
        }catch (\Throwable $e){
            flog([
                'message' => $e->getMessage(),
                'data' => $model->toArray(),
            ],'video_detail_update','error');
        }
    }
}